package com.jwell.panrank.ui.adapter

import android.content.Intent
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.jwell.panrank.R
import com.jwell.panrank.entity.WaybillBean
import com.jwell.panrank.ui.activity.WaybillInfoActivity
import kotlinx.android.synthetic.main.item_waybill_layout.view.*

/**
 * Created by 76577 on 2018/6/11.
 * 运单列表
 */
class WaybillAdapter constructor(list: List<WaybillBean>) : RecyclerView.Adapter<WaybillAdapter.ViewHolder>() {
    private var list = list


    override fun getItemCount(): Int {
        return list.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent?.context).inflate(R.layout.item_waybill_layout, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        with(holder.itemView) {
            text_code.text = list[position].forBillCode ?: ""
            text_goodsName.text = list[position].materialName ?: ""

            text_receiptCom.text = list[position].receivingUnit ?: ""
            text_driverInfo.text = (list[position].driverName ?: "") + "/" + (list[position].carNum ?: "")
            text_waybill_state.text = list[position].appBillStateName ?: ""
            // (integer, optional): 计量类别 1、外购-辅料 2、外购-其它 3、外购-大宗原燃料 4、外销 5、厂际 6、其它 = ['1', '2', '3', '4', '5', '6'],
            when (list[position].measurementType) {
                1 -> text_waybillType.text = "外购-辅料"
                2 -> text_waybillType.text = "外购-其他"
                3 -> text_waybillType.text = "外购-大宗原燃料"
                4 -> text_waybillType.text = "外销"
                5 -> text_waybillType.text = "厂际"
                6 -> text_waybillType.text = "其他"
                else -> {
                    text_waybillType.text = ""
                }
            }
            if (list[position].measurementType == 3) {
                text_tag_1.text = "供"
                text_tag_1.setBackgroundResource(R.drawable.bg_text_green)
                text_tag_1.setTextColor(resources.getColor(R.color.green))
                text_startCom.text = list[position].supplierName ?: ""
            } else {
                text_tag_1.text = "发"
                text_tag_1.setBackgroundResource(R.drawable.bg_text_red)
                text_tag_1.setTextColor(resources.getColor(R.color.red))
                text_startCom.text = list[position].forwardingUnit ?: ""
            }
            when (list[position].billState) {
                1, 4, 6, 7, 8 -> {//运输中,入库中,排号失效,超时未送达,申请延期
                    text_describe.text = ""
                    text_describe.visibility = View.GONE
                }
                2, 3 -> {//正在排号,缓冲区等待
                    text_describe.text = "号数:" + (list[position].seQ_ID ?: "") + "\n" + "等待人数:" + list[position].waitCount + "人"
                    text_describe.visibility = View.VISIBLE
                }
                5 -> {//已收料
                    text_describe.text = "净重:" + list[position].netWeight + "吨"
                    text_describe.visibility = View.VISIBLE
                }
                9 -> {//已作废
                    text_describe.visibility = View.VISIBLE
                    text_describe.text = "(" + list[position].toVoidRemark + ")"
                }
                else -> {
                    text_describe.text = ""
                    text_describe.visibility = View.GONE
                }
            }
            holder.itemView.setOnClickListener {
                val intent = Intent(holder.itemView.context, WaybillInfoActivity::class.java)
                intent.putExtra("id", list[position].id)
                holder.itemView.context.startActivity(intent)
            }
        }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
}